<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>字典明细管理</title>
    <#-- 使用相对当前模板文件的路径 再去找另一个模板文件 -->
    <#include "/common/link.ftl">


</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--定义一个变量  菜单回显-->
    <#assign currentMenu="systemDictionaryItem"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>字典明细管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <div class="row" style="margin:20px">
                    <div class="col-xs-2">
                        <div class="panel panel-default">
                            <div class="panel-heading">字典目录</div>
                            <div class="panel-body">
                                <div class="list-group" id="dic">
                                    <#list systemDictionaries as sys>
                                        <a href="/systemDictionaryItem/list?typeId=${sys.id}" class="list-group-item"
                                           data-id="${sys.id}">${sys.title}</a>
                                    </#list>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-10">
                        <!--高级查询--->
                        <form class="form-inline" id="searchForm" action="/systemDictionaryItem/list" method="post">
                            <input type="hidden" name="currentPage" id="currentPage" value="1">
                            <#--因为没有设置默认的typeId的值 如果点击下一页 就会丢失typeId 的值 所以我们要定义一个标签来储存
                            每一次传递的typeId 都是作为过滤条件 所以我们可以使用qo来获取-->
                            <input type="hidden" name="typeId" id="typeId" value="${qo.typeId}">
                            <input type="hidden" name="parentId" id="parentId" value="${qo.parentId}">
                            <a href="#" class="btn btn-success btn-input" style="margin: 10px">
                                <span class="glyphicon glyphicon-plus"></span> 添加
                            </a>
                        </form>
                        <!--编写内容-->
                        <div class="box-body table-responsive no-padding ">
                            <table class="table table-hover table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>字典明细标题</th>
                                    <th>字典明细序号</th>
                                    <th>上级明细</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <#list pageInfo.list as sysItem>
                                    <tr>
                                        <td>${sysItem_index + 1}</td>
                                        <td>
                                            <a href="/systemDictionaryItem/list?parentId=${sysItem.id}">${sysItem.title}</a>
                                        </td>
                                        <td>${sysItem.sequence}</td>
                                        <#if sysItem.parent.title == null>
                                            <td>无</td>
                                        </#if>
                                        <#if sysItem.parent.title != null>
                                            <td>${sysItem.parent.title}</td>
                                        </#if>
                                        <td>
                                            <a href="#" class="btn btn-info btn-xs btn-input"
                                               data-json='${sysItem.toJSON()}'>
                                                <span class="glyphicon glyphicon-pencil"></span> 编辑
                                            </a>
                                        </td>
                                    </tr>
                                </#list>
                                </tbody>
                            </table>
                            <#include "/common/page.ftl" >
                        </div>
                    </div>
                </div>

            </div>
        </section>
    </div>
    <#include "/common/footer.ftl" >
</div>


<!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
            </div>
            <form class="form-horizontal" action="/systemDictionaryItem/saveOrUpdate" method="post" id="editForm">
                <div class="modal-body">
                    <input type="hidden" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label">字典目录：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="type.id" id="typeSelect">
                                <option value="">请选择</option>
                                <#list systemDictionaries as sys>
                                    <option value="${sys.id}">${sys.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label class="col-sm-3 control-label">上级明细：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="parent.id" id="parentSelect">
                                <option value="">无</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label class="col-sm-3 control-label">明细标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"
                                   placeholder="请输入字典明细编码" name="title">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label class="col-sm-3 control-label">明细序号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"
                                   placeholder="请输入字典明细序号" name="sequence">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary btn-submit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>

    /*如果qo.typeId不为null*/
    <#if qo.typeId??>
    /*当a标签的data-id 的值为qo.typeId的值的时候 就高亮*/
    $('a[data-id=${qo.typeId}]').addClass('active');
    </#if>

    //当点击编辑的时候
    $('.btn-input').click(function () {
        $('#editForm input').val('');
        $('#editForm select').val('');

        //先获取data-json的数据
        let json = $(this).data('json');
        if (json) {
            $('#editForm input[name = id]').val(json.id);
            $('#editForm input[name = title]').val(json.title);
            $('#editForm input[name = sequence]').val(json.sequence);
            $('#editForm select[name = "type.id"]').val(json.typeId);
            if(json.parentId != null){
                $('#editForm select[name = "parent.id"]').html('<option value="'+json.parentId+'">'+json.parentTitle+'</option>')
            }else {
                $('#editForm select[name = "parent.id"]').html('<option value="">无</option>')
            }
 /*           $('#editForm select[name = "parent.id"]').val(json.parentId);*/
        }
        $('#editModal').modal('show')

    });

    /*为字典目录设置改变事件*/
    $('#typeSelect').change(function () {
        //获取当前的id
        var id = $(this).val();
        //定义一个var str 来接收筛选的数据
        var str ='<option value="">无</option>';
        $.get("/systemDictionaryItem/selectByTypeId" , {id:id},function (data) {
            $.each(data,function (id,ele) {
                str += '<option value="'+ele.id+'">'+ele.title+'</option>'
            });
            $('#parentSelect').html(str);
        })
    });

    $('#editForm').ajaxForm(function (data) {
        if (data.success) {
            //弹框表示加载成功,当用户点击确定,重新刷新页面
            Swal.fire({
                text: "操作成功",
                icon: 'success',
                confirmButtonText: 'ok',
            }).then((result) => {
                if (result.value) {
                    window.location.reload();
                }
            })
            //如果加载失败
        } else {
            Swal.fire(
                data.msg,
                '',
                'error',
            )
        }
    })

</script>
</body>
</html>
